// component/articlelist/articlelist.js
import { 
  SBU_LIST,
  GET_ARTICLES,
  BANNER,
 } from "../../request/api";

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    listData:{
      type: Object,
      observer: function(newVal, oldVal){
        if(newVal.listviewHeight && newVal.listviewHeight > 0){
          this.setData({
            listviewData: {
              height: newVal.listviewHeight,
            },
          });
        }
        if(newVal.hasBanner){
          this.setData({
            hasBanner: newVal.hasBanner,
          });
        }
        if(newVal.catalog){
          this.data.catalog = newVal.catalog;
        }
        if(newVal.token && newVal.type){
          this.setData({
            token: newVal.token,
            type: newVal.type,
          });
        } else if(newVal.token){
          this.setData({
            token: newVal.token,
          });
        }
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    listviewData:{
      loadStatus: 0,
      height: 0,
    },
    dataList:[],
    pageToken: '',
    userId: '',
    token: 'e6142fa662bc4bf21083870a957fbd20',
    type: 1,
    hasBanner: false,
    catalog: 4,
    bannerList: [],
  },

  lifetimes: {
    attached: function(){
      // this.refresh();
    },
    ready: function(){
      this.refresh();
    }
  },
  pageLifetimes: {
    show: function(){},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toDetail: function(e){
      var id = e.currentTarget.dataset.id;
      var type = e.currentTarget.dataset.type;
      wx.navigateTo({
        url: '../../pages/article/detail/detail?id='+id+'&type='+type,
      });
    },
  
    refresh: function(){
      this.data.dataList = [];
      this.data.pageToken = '';
      this.data.bannerList = [];
      this.loadMore();
      this.loadBanner();
    },
    loadMore: function(){
      if(this.data.type == 2){
        this.loadArticleList();
      } else {
        this.loadSubList();
      }
    },
    loadSubList: function(){
      SBU_LIST({'token': this.data.token, 'pageToken': this.data.pageToken}).then(res => {
        if(res.data.code == 1){
          var list = res.data.result.items;
          if(this.data.pageToken){
            list = this.data.dataList.concat(list);
          }
          this.setData({
            dataList: list,
            pageToken: res.data.result.nextPageToken,
            listviewData: {
              loadStatus: res.data.result.responseCount < res.data.result.requestCount? 3 : 2,
            },
          });
        } else {
          this.setLoadStatus(2);
        }
      }).catch(err => {
        this.setLoadStatus(2);
      });
    },
    loadArticleList: function(){
      GET_ARTICLES({'token': this.data.token, 'pageToken': this.data.pageToken}).then(res => {
        if(res.data.code == 1){
          var list = res.data.result.items;
          if(this.data.pageToken){
            list = this.data.dataList.concat(list);
          }
          this.setData({
            dataList: list,
            pageToken: res.data.result.nextPageToken,
            listviewData: {
              loadStatus: res.data.result.responseCount < res.data.result.requestCount? 3 : 2,
            },
          });
        } else {
          this.setLoadStatus(2);
        }
      }).catch(err => {
        this.setLoadStatus(2);
      });
    },
    setLoadStatus: function(loadStatus){
      this.setData({
        listviewData: {
          loadStatus: loadStatus,
        },
      });
    },
    
    loadBanner: function(){
      if(!this.data.hasBanner){
        return;
      }
      BANNER({'catalog': this.data.catalog}).then(res => {
        if(res.data.code == 1){
          this.setData({
            bannerList: res.data.result.items,
          });
        }
      }).catch(err => {

      });
    }
  }
})
